#include "CRL_ShaderBundleDescriptor.h"
#include "CBL_Debug.h"

CB_SUBNAMESPACE_START(RenderLib)

// ---------------------------------------------------------------------------------------------------------------------
//	COpenGLPixelBufferSettings
// ---------------------------------------------------------------------------------------------------------------------

const char* CShaderBundlePixelBufferSettings::s_InterpolatorNames[NumInterpolators] = {
	"Point",
	"Linear",
	"Anisotropic"
};

// ---------------------------------------------------------------------------------------------------------------------

CShaderBundlePixelBufferSettings::CShaderBundlePixelBufferSettings()
	: m_SamplerIndex(0)
	, m_MainInterpolator(Linear)
	, m_MinInterpolator(Point)
	, m_MaxInterpolator(Point)
{
}

// ---------------------------------------------------------------------------------------------------------------------

void CShaderBundlePixelBufferSettings::SERIALIZE_FUNCTION()
{
	_rSerializer.Serialize(m_SamplerIndex, "SamplerIndex", ISerializer::FlagRequiredField);
	
	uint32 MainInterpolator = (uint32)m_MainInterpolator;
	uint32 MinInterpolator  = (uint32)m_MinInterpolator;
	uint32 MaxInterpolator  = (uint32)m_MaxInterpolator;
	
	_rSerializer.Serialize(MainInterpolator, s_InterpolatorNames, NumInterpolators, "Sampling");
	_rSerializer.Serialize(MinInterpolator,  s_InterpolatorNames, NumInterpolators, "MinSampling");
	_rSerializer.Serialize(MaxInterpolator,  s_InterpolatorNames, NumInterpolators, "MaxSampling");
	
	m_MainInterpolator = (EInterpolator)MainInterpolator;
	m_MinInterpolator  = (EInterpolator)MinInterpolator;
	m_MaxInterpolator  = (EInterpolator)MaxInterpolator;
}

// ---------------------------------------------------------------------------------------------------------------------
//	CShaderBundlePass
// ---------------------------------------------------------------------------------------------------------------------

void CShaderBundlePass::SERIALIZE_FUNCTION()
{
	// Shader settings
	
	_rSerializer.Serialize(m_Name, "Name");
	
	_rSerializer.Serialize(m_PixelBufferSettings,  "PixelBufferSampler", "Sampler");
    
	_rSerializer.Serialize(m_VertexIncludes,   "VertexShader", "Source");
	_rSerializer.Serialize(m_GeometryIncludes, "GeometryShader", "Source");
	_rSerializer.Serialize(m_FragmentIncludes, "FragmentShader", "Source");
}

// ---------------------------------------------------------------------------------------------------------------------
//	CShaderBundleTechnique
// ---------------------------------------------------------------------------------------------------------------------

void CShaderBundleTechnique::SERIALIZE_FUNCTION()
{
	_rSerializer.Serialize(m_Name, "Name");
	_rSerializer.SerializeBlockStart("Passes");
	
	if (_rSerializer.IsReading())
	{
		CShaderBundlePass Pass;
		
		while (_rSerializer.Serialize(Pass, "Pass", ISerializer::FlagCollectionItem))				
			m_Passes.push_back(Pass);
	}
	else
	{
		uint32 NumIncludes = m_Passes.size();
		
		for (uint32 i=0; i<NumIncludes; ++i)
		{
			_rSerializer.Serialize(m_Passes[i], "Pass", ISerializer::FlagCollectionItem);
		}
	}
	
	_rSerializer.SerializeBlockEnd("Passes");
}

// ---------------------------------------------------------------------------------------------------------------------
//	CShaderBundleDescriptor
// ---------------------------------------------------------------------------------------------------------------------

void CShaderBundleDescriptor::SERIALIZE_FUNCTION()
{
	CShaderSourceDescriptor::Serialize(_rSerializer);
	_rSerializer.SerializeBlockStart("Techniques");
	
	if (_rSerializer.IsReading())
	{
		CShaderBundleTechnique Technique;
		
		while (_rSerializer.Serialize(Technique, "Technique", ISerializer::FlagCollectionItem))				
			m_Techniques.push_back(Technique);
	}
	else
	{
		uint32 NumIncludes = m_Techniques.size();
		
		for (uint32 i=0; i<NumIncludes; ++i)
		{
			_rSerializer.Serialize(m_Techniques[i], "Technique", ISerializer::FlagCollectionItem);
		}
	}
	
	_rSerializer.SerializeBlockEnd("Techniques");
}

CB_SUBNAMESPACE_END